<?php
//edit product page

$js = '
function changeMax() {
	var min = parseFloat($F("ProductMinimumWeight"));
	var max = parseFloat($F("ProductMaximumWeight"));
	if (!min && max) { $("ProductMinimumWeight").value = "0"; }
	if (!max) {$("ProductMaximumWeight").value = $F("ProductMinimumWeight"); }
}

function changeMin() {
	var min = parseFloat($F("ProductMinimumWeight"));
	var max = parseFloat($F("ProductMaximumWeight"));
	if (!min) { $("ProductMinimumWeight").value = "0"; }
	if (!max) { $("ProductMinimumWeight").value = $F("ProductMaximumWeight"); }
}

function changeExample() {
	var ex = "(Qty) 1 ";
	var w = $F("ProductRandomWeight");
	var min = parseFloat($F("ProductMinimumWeight"));
	var max = parseFloat($F("ProductMaximumWeight"));
	var price = $F("ProductUnitPrice");
	var ob = $F("ProductOrderingUnit");
	var pb = $F("ProductPricingUnit");
	var t = $F("ProductTaxExempt");
	var final = 0;
	if (!min) { min = 0; }
	if (!max) { max = 0;}
	if (w) {
		var avg = (min + max) / 2;
		if (!avg) {
			avg = 1;
		}
		ex = ex + ob + " X $" + price + " per " + pb + " X " + avg + " " + pb + "(s)";
		final = price * avg;
	} else {
		ex = ex + pb + " X $" + price + " per " + pb;
		final = price;
	}
	if (!t) {//not tax exempt
		ex = ex + " X " + (' . SALES_TAX . ' * 100) + "% sales tax";
		final = final * (1 + ' . SALES_TAX . ');
	}
	
	ex = ex + " = $" + (Math.round(final*100)/100).toFixed(2);
	$("example").innerHTML = ex;
}

function changeUnit() {
	var unit = $F("ProductPricingUnit");
	if (unit) {
		$("unit").innerHTML = unit;
	} else {
		$("unit").innerHTML = "(Pricing Unit)";
	}
}

function showWeightForm() {
	var items = $A($$(".weight"));
	items.each(function(me) {
		Effect.Appear(me);
	});
}

function hideWeightForm() {
	var items = $A($$(".weight"));
	items.each(function(me) {
		Effect.Fade(me);
	});
}

function toggleForm() {
	var w = $F("ProductRandomWeight");
	if (w) {
		//weight form
		showWeightForm();
	} else {
		//flat price form
		hideWeightForm();
	}
}';

echo $javascript->codeBlock($js);

?>

<form method='post' action='<?=$form->url('/products/edit/' . $this->data['Product']['product_id'])?>'>
  <?=$form->hidden("Product.product_id");?>
  
  <fieldset>
    <legend>Product Information</legend>
    <?=$form->input("Product.product_name",array("size"=>50));?>
    <?=$form->input("Product.detailed_notes",array("rows"=>7,"cols"=>50));?>
    <?=$form->input("Product.subcategory_id", array('label'=>'Category','options' => $cats));?>
    <?=$form->input("Product.storage",array('options' => array('shelved' => 'shelved', 'refrigerated' => 'refrigerated', 'frozen' => 'frozen')));?>
    <?=$form->input("Product.inventory_on", array('label'=>'Limit Supply'));?>
    <?=$form->input("Product.inventory",array('label'=>'Qty. Available'));?>
    <p class="help">When "Limited Supply is checked, and Qty. Available is 0, the item will show up as a "Special Order Only" item.  Customers will not be able to order it through the site.</p>
   	<?=$form->input("Product.donotlist", array('type' => 'checkbox', 'label'=>'Do <strong>NOT</strong> List this product'));?>
  </fieldset>
  
  <fieldset>
    <legend>Product Pricing</legend>
    <p>The following might get confusing, so these descriptions are here to help.</p>
    <p>Basically, we want to know if your item needs to be weighed on delivery, and how to
    bill it.  Also, we want the membership to know what they are ordering when they
    place their order, so that both you and the members get what they want.</p>
    <?=$form->input("Product.tax_exempt");?>
    <?=$form->input("Product.random_weight", array('type'=> 'checkbox', 'label' => 'Final Price based on Item Weight?'));?>
    <?=$form->input("Product.pricing_unit", array('label'=>'Priced by the'));?>
    <p class="help">Example: pound, gallon, jar, dozen</p>
    <?=$form->input("Product.unit_price",array('size'=>10, 'label' => 'Price per <span id="unit">(Pricing Unit)</span> ($)'));?>
    <?=$form->input("Product.ordering_unit", array('label'=> 'Ordered by the', 'div'=> array('class'=>'input text weight')));?>
    <p class="weight help">This is used when the member orders by a different measure than the item is priced by.  For example,
    a member buys a <em>whole</em> turkey, but the producer charges by the pound.  In this case, you'd put
    "pound" in the Priced By box, and "turkey" or "bird" in the Ordered By box.</p>
    <?=$form->input("Product.minimum_weight", array('div'=> array('class'=>'input text weight')));?>
    <?=$form->input("Product.maximum_weight", array('div'=> array('class'=>'input text weight')));?>
    <p class="weight help">Use the maximum &amp; minimum weight values at your discresion: they make sense for some products, but not for others.
    These values are to help the member understand the range of sizes this product may come in.</p>
    <p>When a member sees an item in their basket, its pricing will calculate as follows:</p>
    <p id="example">(example)</p>
  </fieldset>
  <?=$form->submit("Save Product Changes");?>
</form>
<?=$javascript->codeBlock('
new Form.Element.EventObserver($("ProductRandomWeight"),toggleForm);
toggleForm();
new Form.Element.EventObserver($("ProductPricingUnit"),changeUnit);
changeUnit();
new Form.Element.EventObserver($("ProductMinimumWeight"),changeMax);
new Form.Element.EventObserver($("ProductMaximumWeight"),changeMin);
var elems = $A($("ProductRandomWeight", "ProductPricingUnit", "ProductOrderingUnit", "ProductTaxExempt", "ProductMinimumWeight","ProductMaximumWeight","ProductUnitPrice"));
elems.each(function(me) {
	new Form.Element.EventObserver(me,changeExample);
});
changeExample();');?>